﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.DAL.SurveyMgr;
using ZkSurvey.DAL;

namespace ZkSurvey.BLL.SurveyMgr
{
    public class QuestionBl
    {
        /// <summary>
        /// 创建问题
        /// </summary>
        /// <param name="question"></param>
        public void CreateQuestion(Question question)
        {
            QuestionDa.InsertQuestion(question);
        }

        /// <summary>
        /// 查询某个问卷的问题列表(同时查询问题的选项列表)
        /// </summary>
        /// <param name="surveyId"></param>
        /// <returns></returns>
        public List<Question> GetQuestionList(int surveyId)
        {
            List<Question> questions = QuestionDa.GetQuestionList(surveyId);
            DataDictionaryBl dicBl = new DataDictionaryBl();
            foreach (Question ques in questions)
            {
                ques.TypeTitle = dicBl.GetQuestionType((int)ques.Type).Title;
                ques.Options = OptionDa.GetOptionList(ques.Id);
            }

            return questions;
        }

        /// <summary>
        /// 根据Id查询问题(同时查询问题的选项列表)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Question GetQuestion(int id)
        {
            Question question = QuestionDa.GetQuestion(id);
            question.TypeTitle = new DataDictionaryBl().GetQuestionType((int)question.Type).Title;
            question.Options = OptionDa.GetOptionList(question.Id);
            return question;
        }

        /// <summary>
        /// 根据问卷编码查询
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<Question> GetQuestionList(Guid code)
        {
            List<Survey> surveies = SurveyDa.GetSurveyList();
            Survey survey = surveies.Find(s => s.Code.Equals(code));
            if (survey == null)
                throw new ApplicationException("不存在查询的调查问卷");

            return QuestionDa.GetQuestionList(survey.Id);
        }

        /// <summary>
        /// 删除问题
        /// </summary>
        /// <param name="quesId"></param>
        public void DeleteQuestion(int quesId)
        {
            //删除问题以及选项
            Question question = QuestionDa.GetQuestion(quesId);
            Survey survey = SurveyDa.GetSurvey(question.SurveyId);
            if (survey.Status == SurveyStatus.Running)
                throw new ApplicationException("问卷调查正在进行，不允许删除!");
            if (survey.Status == SurveyStatus.Completed)
                throw new ApplicationException("问卷调查已经完成，不允许删除!");

            using (TranscationHelper trans = TranscationHelper.GetInstance())
            {
                trans.BeginTrans();
                try
                {

                    List<Option> options = OptionDa.GetOptionList(quesId, trans);
                    foreach (Option opt in options)
                    {
                        OptionDa.Delete(opt.Id, trans);
                    }
                    QuestionDa.DeleteQuestion(quesId, trans);
                    trans.CommitTrans();
                }
                catch
                {
                    trans.RollTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 查询问题的最大序号
        /// </summary>
        /// <param name="surveyId"></param>
        /// <returns></returns>
        public int GetMaxSort(int surveyId)
        {
            List<Question> questions = QuestionDa.GetQuestionList(surveyId);
            if (questions.Count == 0)
                return 0;

            return questions.Max(q => q.Sort);
        }
        
        /// <summary>
        /// 查询问题的下一个序号
        /// </summary>
        /// <param name="surveyId"></param>
        /// <returns></returns>
        public int GetNextSort(int surveyId)
        {
            return GetMaxSort(surveyId) + 1;
        }

        /// <summary>
        /// 更新问题
        /// </summary>
        /// <param name="question"></param>
        public void UpdateQuestion(Question question)
        {
            using (TranscationHelper trans = TranscationHelper.GetInstance())
            {
                trans.BeginTrans();
                try
                {
                    QuestionDa.UpdateQuestion(question, trans);

                    foreach (Option opt in question.Options)
                    {
                        OptionDa.UpdateOption(opt, trans);
                    }

                    trans.CommitTrans();
                }
                catch
                {
                    trans.RollTrans();
                    throw;
                }
            }
        }
    }
}
